package com.gri.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GriUtils {
	
	public final static String UA_IE7 =
			"Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)";

	private static Properties props;
	
	public static String readInputStream(InputStream is){
		if (null == is)
			return "";
		BufferedReader reader =null;
		StringBuilder sb = new StringBuilder(); 
		try{
			reader = new BufferedReader(new InputStreamReader(is));			
			String inputLine;
	        while ((inputLine = reader.readLine()) != null)
	        	sb.append(inputLine);
	        
		}catch(Exception ex){
			System.out.println("Error occured while reading the Input stream " + ex);
		}finally{
			if(reader!=null){
				try{
				reader.close();
				}catch(Exception ex){
					System.out.println("Error occured while closing the reader " + ex);
				}
			}
		}
        
        return sb.toString();
	}
	
	public static InputStream hitUrlAndGetSearchInInputStream(String url){
		InputStream retStream = null;
		HttpURLConnection httpConnection = null;
		try{
			URL searchURL =new URL(url);
			// Open connection
			httpConnection=(HttpURLConnection) searchURL.openConnection();
	
	    	// Set User-Agent request header
	    	httpConnection.setRequestProperty("User-Agent", UA_IE7);
	
	    	// HTTP response code (200 means success)
	    	if(httpConnection.getResponseCode()==200){
	    		// Open input stream on the search result page
	    		retStream = httpConnection.getInputStream();
	    	}
		}catch(Exception ex){
			System.out.println("Error occured while hitting the url" + ex);
		}
    	
    	return retStream;
	}
	
	public static Boolean isLoggedIn(HttpServletRequest request){
		String isLoggedIn = (String)request.getSession().getAttribute("isloggedIn");
		if(isLoggedIn != null){
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}
	
	public static void forwardToPage(HttpServletRequest request, 
			HttpServletResponse response, String pageRelatePath){
		RequestDispatcher rd = request.getRequestDispatcher(pageRelatePath);
		try {
			rd.forward(request, response);
		} catch (Exception ex){
		}
	}
	
	public static void redirectToPage(HttpServletRequest request, 
			HttpServletResponse response, String pageRelatePath){
		try {
			response.sendRedirect(pageRelatePath);
		} catch (Exception ex){
		}
	}
	
	public static Connection getConnection(){
		Connection con =null;
		try{
		Class.forName(getStringProperty("jdbcDriver"));
		con = DriverManager.getConnection(getStringProperty("datasource"));
		}catch(Exception ex){
			System.out.println(ex);
		}
		return con;
	}
	
	public static void setPropertiesInServletContext(ServletContext context){
		try{
			InputStream inputStream = context.getResourceAsStream("/WEB-INF/classes/GRI.properties");
			props = new Properties();
			props.load(inputStream);
			//context.setAttribute("props", props);
			System.out.println("property file loaded from Utils");
		}catch(Exception ex){
			System.out.println("Problem in loading the property file");
		}
	}
	
	public static String getStringProperty(String key){
		try{
			return props.getProperty(key);
		}catch(Exception ex){
			System.out.println("Error occured while retrieving the property value : " + key);
		}
		return "";
	}
	
	public static Integer getIntProperty(String key){
		try{
			return Integer.parseInt(props.getProperty(key));
		}catch(Exception ex){
			System.out.println("Error occured while retrieving the property value : " + key);
		}
	return 0;
	}
}

